package com.glory.wxelectricitypda.adapter;

import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;


import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.bean.DefectCodeBean;
import com.glory.wxelectricitypda.bean.LotBean;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.List;

public class ItemBomInfoAdapter extends BaseQuickAdapter<LotBean.BomInfoBean, BaseViewHolder> {

    private boolean isCreate;
    public ItemBomInfoAdapter(@Nullable ArrayList<LotBean.BomInfoBean> data,boolean isCreate) {
        super(R.layout.item_lines, data);
        this.isCreate = isCreate;
    }

    @Override
    protected void convert(BaseViewHolder helper, LotBean.BomInfoBean item) {
        helper.setText(R.id.tvMaterialName,item.getMATERIALNAME());
        helper.setText(R.id.tvMaterialDesc,item.getMATERIALDESC());
        helper.setText(R.id.tvMaterialType,item.getMATERIALTYPE());
        if (!isCreate){
            if (item.getMATERIALLOT().equals("")){
                helper.setGone(R.id.etMaterialLot,true);
                helper.setGone(R.id.tvMaterialLot,false);
            }else {
                helper.setGone(R.id.etMaterialLot,false);
                helper.setGone(R.id.tvMaterialLot,true);
                helper.setText(R.id.tvMaterialLot,item.getMATERIALLOT());
            }
        }

        FreeUI_ClearEditText etMaterialLot = helper.getView(R.id.etMaterialLot);

        etMaterialLot.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(final CharSequence s, int start, int before, int count) {
                if (start == 0 && before == 0 && count > 1) {
                    String text = "";
                    if (s.toString().contains("|")) {
                        text = s.toString().replace("|", ",").split(",")[2];
                    }else {
                        text = s.toString();
                    }
                    etMaterialLot.setText(text);
                    item.setMATERIALLOT(text);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

//        setEditListener(etMaterialLot,item);
    }



    private void setEditListener(EditText editText, LotBean.BomInfoBean item) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    String text = "";
                    if (s.toString().contains("|")) {
                        text = s.toString().replace("|", ",").split(",")[1];
                    }else {
                        text = s.toString();
                    }
                    editText.setText(text);
                    item.setMATERIALLOT(text);
                }catch (NumberFormatException e){
                    e.printStackTrace();
                }

            }
        };

        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }

    public void isCreate(boolean b) {
        this.isCreate = b;
    }
}
